// pages/frontFee/frontFee.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payType: 0,
    renewIdx:'',
    renewType: ['正常续费 ', '预存', '停课'],
    stockArr:[],
    stockIdx:'',
    stockIdx0: '',
    stockIdx1: '',
    stockIdx2: '',
    stockIdx3: '',
    specArr:[],
    specIdx:'',
    specIdx0: '',
    specIdx1: '',
    specIdx2: '',
    specIdx3: '',
    addArr:[]
  },

  //缴费方式
  selectType(e) {
    var payType = e.currentTarget.id;
    this.setData({
      payType
    })

  },

  //续费开始时间选择
  selectSignTime(e) {
    this.setData({
      signTime: e.detail.value
    })
  },

  //续费结束时间选择
  selectDueDate(e) {
    this.setData({
      dueDate: e.detail.value
    })
  },

  //选择续费类型
  selectDenew(e){
    this.setData({
      renewIdx: e.detail.value
    })
  },

  //加库存
  addStock(e) {
    // console.log(e.currentTarget.dataset.id)
    var addArr = this.data.addArr;
    addArr.push(1);
    if (addArr.length >= 4) {
      addArr = addArr.slice(0, 4)
      console.log(addArr)
    }
    this.setData({
      addArr
    })
  },

  //减库存
  reduceStock() {
    var addArr = this.data.addArr;
    addArr = addArr.slice(0, -1);
    console.log(addArr)
    this.setData({
      addArr
    })
  },

  //表单提交
  formSubmit(e){
    console.log(e.detail.value);
    if (!this.data.stockId) { this.data.stockId = '' };
    if (!this.data.specId) { this.data.specId = '' };
    if (!this.data.stockId0) { this.data.stockId0 = '' };
    if (!this.data.specId0) { this.data.specId0 = '' };
    if (!this.data.stockId1) { this.data.stockId1 = '' };
    if (!this.data.specId1) { this.data.specId1 = '' };
    if (!this.data.stockId2) { this.data.stockId2 = '' };
    if (!this.data.specId2) { this.data.specId2 = '' };
    if (!this.data.stockId3) { this.data.stockId3 = '' };
    if (!this.data.specId3) { this.data.specId3 = '' };
    var spec = "" + this.data.stockId + ":" + "" + this.data.specId + "," + "" + this.data.stockId0 + ":" + "" + this.data.specId0 + "," + "" + this.data.stockId1 + ":" + "" + this.data.specId1 + "," + "" + this.data.stockId2 + ":" + "" + this.data.specId2 + "," + "" + this.data.stockId3 + ":" + "" + this.data.specId3
    var formValue = e.detail.value;
    var renew_type = formValue.renew_type == "0" ? 0 : formValue.renew_type == "1" ? 1 : formValue.renew_type == "2"?2:3
    console.log(renew_type)
    var params={
      token: wx.getStorageSync('token'),
      id: this.data.studentDetail[0].id,
      tuition: formValue.tuition,
      renew_type,
      remark: formValue.remark,
      due_date: formValue.due_date,
      pay_type: this.data.payType,
      school_hour: formValue.school_hour || 0,
      // stock_id: this.data.stockId || 0,
      spec
    }

    if (this.data.stockIdx !== '' && this.data.specIdx === '') {
      api.showToast('请填写规格', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!formValue.due_date) {
      api.showToast('请填写到期时间', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (this.data.renewIdx==='') {
      api.showToast('请填写续费类型', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!formValue.tuition) {
      api.showToast('请填写续费金额', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    
    api.fetchApi('reception/translate',params).then(res=>{
      api.showToast('续费成功').then(()=>{
        setTimeout(()=>{
          wx.navigateBack({
            
          })
        },1500)
      })
    })
  },


//获取库存列表
getStockList(){
  api.fetchApi('reception/getstock', { token: wx.getStorageSync('token'), campus: this.data.studentDetail[0].campus_id }).then(res => {
    this.setData({
      stockArr: res,
      // stockIdx: '',
      // specIdx: '',
      // specArr: []
    })
  })
},

  //选择库存
  selectStock(e) {
    var idx = e.detail.value;
    var nowArr = this.data.stockArr[idx]
    console.log(nowArr)
    this.setData({
      stockIdx: idx,
      stockId: nowArr.id
    })

    //后台会返回规格
    api.fetchApi('reception/getspec', { token: wx.getStorageSync('token'), id: nowArr.id }).then(res => {
      this.setData({
        specArr: res,
        specIdx: ''
      })
    })
  },

  //选择规格
  selectSpec(e) {
    var idx = e.detail.value;
    var nowArr = this.data.specArr[idx];
    this.setData({
      specIdx: idx,
      specId: nowArr.id,
    })
  },
  

  //送第二个
  selectStock0(e) {
    var idx = e.detail.value;
    var nowArr = this.data.stockArr[idx]
    console.log(nowArr)
    this.setData({
      stockIdx0: idx,
      stockId0: nowArr.id
    })

    //后台会返回规格
    api.fetchApi('reception/getspec', { token: wx.getStorageSync('token'), id: nowArr.id }).then(res => {
      this.setData({
        specArr0: res,
        specIdx0: ''
      })
    })
  },

  selectSpec0(e) {
    var idx = e.detail.value;
    var nowArr = this.data.specArr0[idx];
    this.setData({
      specIdx0: idx,
      specId0: nowArr.id,
    })
  },

  //送第三个
  selectStock1(e) {
    var idx = e.detail.value;
    var nowArr = this.data.stockArr[idx]
    console.log(nowArr)
    this.setData({
      stockIdx1: idx,
      stockId1: nowArr.id
    })

    //后台会返回规格
    api.fetchApi('reception/getspec', { token: wx.getStorageSync('token'), id: nowArr.id }).then(res => {
      this.setData({
        specArr1: res,
        specIdx1: ''
      })
    })
  },

  selectSpec1(e) {
    var idx = e.detail.value;
    var nowArr = this.data.specArr1[idx];
    this.setData({
      specIdx1: idx,
      specId1: nowArr.id,
    })
  },

  //送第四个
  selectStock2(e) {
    var idx = e.detail.value;
    var nowArr = this.data.stockArr[idx]
    console.log(nowArr)
    this.setData({
      stockIdx2: idx,
      stockId2: nowArr.id
    })

    //后台会返回规格
    api.fetchApi('reception/getspec', { token: wx.getStorageSync('token'), id: nowArr.id }).then(res => {
      this.setData({
        specArr2: res,
        specIdx2: ''
      })
    })
  },

  selectSpec2(e) {
    var idx = e.detail.value;
    var nowArr = this.data.specArr2[idx];
    this.setData({
      specIdx2: idx,
      specId2: nowArr.id,
    })
  },

  //送第五个
  selectStock3(e) {
    var idx = e.detail.value;
    var nowArr = this.data.stockArr[idx]
    console.log(nowArr)
    this.setData({
      stockIdx3: idx,
      stockId3: nowArr.id
    })

    //后台会返回规格
    api.fetchApi('reception/getspec', { token: wx.getStorageSync('token'), id: nowArr.id }).then(res => {
      this.setData({
        specArr3: res,
        specIdx3: ''
      })
    })
  },

  selectSpec3(e) {
    var idx = e.detail.value;
    var nowArr = this.data.specArr3[idx];
    this.setData({
      specIdx3: idx,
      specId3: nowArr.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   // console.log(options.studentId)
    var studentDetail = JSON.parse(options.studentDetail)
    this.setData({
      studentDetail
    })
    console.log(this.data.studentDetail)

    this.getStockList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})